package com.guli;

import com.guli.gulimall.test.TestApp;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;
import java.util.concurrent.*;

/**
 * @author WZH
 * @title: TwoTask
 * @projectName gulimall-java
 * @description:
 * @date 2022/1/12
 */
@SpringBootTest(classes = TestApp.class)
@RunWith(SpringRunner.class)
public class TwoTask {
    //创建线程池
    private ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(7, 20, 3, TimeUnit.MINUTES
            , new LinkedBlockingDeque<Runnable>(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());

    public void t1() throws ExecutionException, InterruptedException {
        //单线程创建
//        new Thread(()->{
//            System.out.println(Thread.currentThread().getName()+"子线程开始。。。。");
//            System.out.println(Thread.currentThread().getName()+"子线程结束。。。。");
//        }).start();
        long start = System.currentTimeMillis();
        System.out.println(Thread.currentThread().getName()+"主线程开始了"+new Date().getSeconds());

        //.查sku详情   0.5s
        CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName()+"线程1执行");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"线程1结束。。。。");
            return 13;
        }, poolExecutor);
        //2.查sku图片信息 1s
        CompletableFuture<Void> f2 = f1.thenAcceptAsync((t) -> {
            System.out.println(Thread.currentThread().getName()+"线程2执行"+t);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"线程2结束。。。。");
        }, poolExecutor);
        //3.查spu相关信息销售属性 1s
        CompletableFuture<Void> f3 = f1.thenAcceptAsync((t) -> {
            System.out.println(Thread.currentThread().getName()+"线程3执行"+t);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"线程3结束。。。。");
        }, poolExecutor);
        //4.查spu商品介绍 1s
        CompletableFuture<Void> f4 = f1.thenAcceptAsync((t) -> {
            System.out.println(Thread.currentThread().getName()+"线程4执行"+t);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"线程4结束。。。。");
        }, poolExecutor);
        //5.查规格和包装 05s
        CompletableFuture<Void> f5 = f1.thenAcceptAsync((t) -> {
            System.out.println(Thread.currentThread().getName()+"线程5执行"+t);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"线程5结束。。。。");
        }, poolExecutor);
        //6.查秒杀降价信息  1.5s
        CompletableFuture<Void> f6 = CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName()+"线程6执行");
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"线程6结束。。。。");
        }, poolExecutor);
        CompletableFuture<Void> allOf = CompletableFuture.allOf(f1,f2,f3,f4,f5,f6);
        long end = System.currentTimeMillis();
        System.out.println(Thread.currentThread().getName()+"主线程结束了"+(end-start)+allOf.get()+new Date().toLocaleString());
    }
}
